exports.addSales = function(req, res) {

	//获取参数信息
	var bodyParams = req.routeInfo.bodyParams;

	//新增的用户ID
	var newUserId = 0;

	//判断该手机号是否已经被使用
	function validationPhone(cb) {

		R.SEND_HTTP(
			req,
			res, {
				url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_username/" + bodyParams.u_phone,
				method: 'GET'
			},
			function(data) {

				if(data.result.data.length > 0) {
					return RES.errorResponse(res, "该手机号已被录入，请勿重复录入！");
				}

				cb();

			}
		)

	}

	//新增销售人员
	function addSales(cb) {

		R.SEND_HTTP(
			req,
			res, {
				url: CORE_APIConfig.coreAPI.userAdd.Request_Api_Url,
				method: 'POST',
				data: {
					u_username: bodyParams.u_phone,
					u_password: '123456',
					u_nickname: bodyParams.u_name,
					u_phone: bodyParams.u_phone,
					u_create_time: common.nowTime(false),
					u_is_activation: 1,
					u_type: bodyParams.os_type,
					u_leader: bodyParams.u_leader,
					u_is_manager: bodyParams.u_is_manager,
					u_master_osid: bodyParams.os_id
				}
			},
			function(data) {

				newUserId = data.result;

				cb();
			}
		)

	}

	//将该员工加入节点
	function addUserToNode(cb) {

		R.SEND_HTTP(
			req,
			res, {
				url: CORE_APIConfig.coreAPI.userOrganizationalMiddleAdd.Request_Api_Url,
				method: 'POST',
				data: {
					uom_osid: bodyParams.os_id,
					uom_uid: newUserId

				}
			},
			function(data) {
				cb();

			}
		)
	}

	//修改销售组长ID
	function updateSales(cb) {

		if(bodyParams.u_is_manager == 1) {

			R.SEND_HTTP(
				req,
				res, {
					url: CORE_APIConfig.coreAPI.userUpdate.Request_Api_Url,
					method: 'POST',
					data: {
						u_leader: newUserId,
            u_id: newUserId,
            update_where: "u_id",
					}
				},
				function(data) {
					cb();
				}
			)

		} else {
			cb();
		}

	}

	F.Serial([validationPhone, addSales, addUserToNode,updateSales], () => {
		RES.successResponse(res);
	})

}